package com.punch.order.controller;


import com.alibaba.fastjson.JSONObject;
import com.punch.common.dto.order.OrderDTO;
import com.punch.common.model.Result;
import com.punch.common.page.PageInfo;
import com.punch.common.query.order.OrderAPPQueryInfo;
import com.punch.common.utils.IpAddressUtil;
import com.punch.common.vo.order.OrderAppDetailVO;
import com.punch.common.vo.order.OrderAppPageVO;
import com.punch.order.service.CommonOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 通用订单 前端控制器
 * </p>
 *
 * @author wjj
 * @since 2020-12-09
 */
@RestController
@Api(value = "CommonOrderController", tags = "订单管理Controller")
public class CommonOrderController {

    @Resource
    private CommonOrderService commonOrderService;

    /**
     * APP端创建订单
     *
     * @param orderDTO 创建订单
     * @param request  请求
     * @return json
     */
    @PostMapping(value = "/order/app", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "APP端创建订单", notes = "APP端创建订单", produces = "application/json")
    public Result<JSONObject> createOrder(@Valid @RequestBody OrderDTO orderDTO, HttpServletRequest request) {
        String ip = IpAddressUtil.getIpAdrress(request);
        orderDTO.setClientIp(ip);
        return Result.succeed(commonOrderService.createOrder(orderDTO));
    }

    /**
     * APP端查询订单
     *
     * @param queryInfo 查询条件
     * @return PageInfo<OrderAppPageVO>
     */
    @GetMapping(value = "/order/app", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "APP端查询订单", notes = "APP端查询订单", produces = "application/json")
    public Result<PageInfo<OrderAppPageVO>> listOrder(@ModelAttribute OrderAPPQueryInfo queryInfo) {
        return Result.succeed(commonOrderService.listOrder(queryInfo));
    }

    /**
     * 获取订单详情
     *
     * @param orderId 订单id
     * @return OrderAppDetailVO
     */
    @GetMapping(value = "/order/app/{orderId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "获取订单详情", notes = "获取订单详情", produces = "application/json")
    public Result<OrderAppDetailVO> getOrder(@PathVariable Long orderId) {
        return Result.succeed(commonOrderService.getOrder(orderId));
    }

    /**
     * 开班修改订单状态
     *
     * @param classId 课程班级id
     * @return boolean
     */
    @PutMapping(value = "/order/class/{classId}/start-class", produces = "application/json; charset=utf-8")
    public Boolean startClass(@PathVariable("classId") Long classId) {
        return commonOrderService.startClass(classId);
    }
}
